package com.binance.connector.client;

import com.binance.connector.client.exceptions.BinanceClientException;
import com.binance.connector.client.exceptions.BinanceConnectorException;
import com.binance.connector.client.impl.SpotClientImpl;
import com.binance.connector.client.utils.DateUtils;
import com.binance.connector.client.utils.JSONParser;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public final class NewOrderBuy {
    private NewOrderBuy() {
    }
    public static SpotClientImpl client = null;


    public static void buyCoinByQty(SpotClientImpl client,long preoidTime){
        String status = "";
        while (true){
            //抢币
            LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
            parameters.put("symbol", PrivateConfig.SYMBOL);
            parameters.put("side", "BUY");
            parameters.put("type", "MARKET");
            parameters.put("quoteOrderQty", PrivateConfig.QTYBUY);
            try {
                String result = client.createTrade().newOrder(parameters);
                status = JSONParser.getJSONStringValue(result,"status");
                System.out.println("请求后："+DateUtils.dateTimeNow()+" "+result);
            } catch (Exception e) {
                System.err.println("请求后"+DateUtils.dateTimeNow()+(String) String.format("fullErrMessage: %s", e.getMessage()));
            }
            //币安限制10秒内只能请求50次，设置线程100毫秒
            try {
                TimeUnit.MILLISECONDS.sleep(preoidTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取指定时间对应的毫秒数
     *
     * @param time "HH:mm:ss"
     * @return
     */
    private static long getTimeMillis(String time)
    {
        try
        {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss:SSS");
            DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
            Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
            return curDate.getTime();
        }
        catch (ParseException e)
        {
        }
        return 0;
    }

    public static void main(String[] args){
        long oneDay = 24 * 60 * 60 * 1000;
//        long initDelay = getTimeMillis(args[0]) - System.currentTimeMillis()-Long.valueOf(args[1]);//提前多少毫秒
//        initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
        client = new SpotClientImpl(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY, PrivateConfig.BASE_URL);
        if (client != null){
            System.out.println("币安抢币客户端连接建立成功！！");
        }
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
        //1
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client,Long.valueOf(args[0]));
            }
        }, 0,oneDay,TimeUnit.MILLISECONDS);
    }
}
